<?php
/**
 * 提现订单模型
 * @author 王勇程 <laughskydragon@qq.com>
 */

namespace app\common\model;

class WithdrawOrder extends ModelBase
{
    /**
     * 获取处理时间格式化
     */
    public function getProcessTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
    
    /**
     * 获取提现统计数据
     * @param int|null $startTime 开始时间戳
     * @param int|null $endTime 结束时间戳
     * @return array
     * @author 王勇程 <laughskydragon@qq.com>
     */
    public function getWithdrawStatistics($startTime = null, $endTime = null)
    {
        $stats = [];
        
        // 构建基础查询条件
        $baseWhere = ['status' => 1]; // 只统计启用状态的记录
        
        // 添加时间范围条件
        if ($startTime && $endTime) {
            $baseWhere['create_time'] = ['between', [$startTime, $endTime]];
        } elseif ($startTime) {
            $baseWhere['create_time'] = ['>=', $startTime];
        } elseif ($endTime) {
            $baseWhere['create_time'] = ['<=', $endTime];
        }
        
        // 总提现订单数
        $stats['total_orders'] = $this->stat($baseWhere, 'count');
        
        // 总提现金额
        $stats['total_amount'] = $this->getValue($baseWhere, 'SUM(amount)') ?: 0;
        
        // 总实际到账金额
        $stats['total_actual_amount'] = $this->getValue($baseWhere, 'SUM(actual_amount)') ?: 0;
        
        // 总手续费
        $stats['total_fee'] = $this->getValue($baseWhere, 'SUM(fee)') ?: 0;
        
        // 各状态统计
        // 待审核数量 (withdraw_status = 0)
        $pendingWhere = array_merge($baseWhere, ['withdraw_status' => 0]);
        $stats['pending_count'] = $this->stat($pendingWhere, 'count');
        $stats['pending_amount'] = $this->getValue($pendingWhere, 'SUM(amount)') ?: 0;
        
        // 处理中数量 (withdraw_status = 1)
        $processingWhere = array_merge($baseWhere, ['withdraw_status' => 1]);
        $stats['processing_count'] = $this->stat($processingWhere, 'count');
        $stats['processing_amount'] = $this->getValue($processingWhere, 'SUM(amount)') ?: 0;
        
        // 已完成数量 (withdraw_status = 2)
        $completedWhere = array_merge($baseWhere, ['withdraw_status' => 2]);
        $stats['completed_count'] = $this->stat($completedWhere, 'count');
        $stats['completed_amount'] = $this->getValue($completedWhere, 'SUM(amount)') ?: 0;
        
        // 已失败数量 (withdraw_status = 3)
        $failedWhere = array_merge($baseWhere, ['withdraw_status' => 3]);
        $stats['failed_count'] = $this->stat($failedWhere, 'count');
        $stats['failed_amount'] = $this->getValue($failedWhere, 'SUM(amount)') ?: 0;
        
        // 如果没有指定时间范围，则计算今日和本月统计
        if (!$startTime && !$endTime) {
            // 今日统计
            $todayStart = strtotime(date('Y-m-d 00:00:00'));
            $todayEnd = strtotime(date('Y-m-d 23:59:59'));
            $todayWhere = [
                'status' => 1,
                'create_time' => ['between', [$todayStart, $todayEnd]]
            ];
            $stats['today_orders'] = $this->stat($todayWhere, 'count');
            $stats['today_amount'] = $this->getValue($todayWhere, 'SUM(amount)') ?: 0;
            $stats['today_completed_amount'] = $this->getValue(array_merge($todayWhere, ['withdraw_status' => 2]), 'SUM(amount)') ?: 0;
            
            // 本月统计
            $monthStart = strtotime(date('Y-m-01 00:00:00'));
            $monthEnd = strtotime(date('Y-m-t 23:59:59'));
            $monthWhere = [
                'status' => 1,
                'create_time' => ['between', [$monthStart, $monthEnd]]
            ];
            $stats['month_orders'] = $this->stat($monthWhere, 'count');
            $stats['month_amount'] = $this->getValue($monthWhere, 'SUM(amount)') ?: 0;
            $stats['month_completed_amount'] = $this->getValue(array_merge($monthWhere, ['withdraw_status' => 2]), 'SUM(amount)') ?: 0;
        }
        
        return $stats;
    }
    
}